package com.atguigu.yygh.controller.api;

import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.service.DepartmentService;
import com.atguigu.yygh.service.HospitalService;
import com.atguigu.yygh.service.HospitalSetService;
import com.atguigu.yygh.service.ScheduleService;
import com.atguigu.yygh.util.SignValidUtils;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zr
 * @date 2021/12/12 16:12
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Resource
    private HospitalService hospitalService;
    @Resource
    private SignValidUtils signValidUtils;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private ScheduleService scheduleService;

    @ApiOperation("删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
//        // 校验
        Map<String, Object> paramMap = signValidUtils.signValidation(request);
//        // 根据医院编号 和排班Id删除排班
        String hosCode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        scheduleService.removeSchedule(hosCode, hosScheduleId);
        return Result.ok();
    }

    @ApiOperation(value = "查询排班")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = signValidUtils.signValidation(request);
        String hosCode = (String) paramMap.get("hoscode");
        Integer page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        Integer limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hosCode);
        Page<Schedule> schedulePage = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(schedulePage);
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        // 签名校验
        Map<String, Object> paramMap = signValidUtils.signValidation(request);
        // 上传
        scheduleService.saveSchedule(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 校验
        Map<String, Object> paramMap = signValidUtils.signValidation(request);
        String hosCode = (String) paramMap.get("hoscode");
        String depCode = (String) paramMap.get("depcode");
        departmentService.removeDepartment(hosCode, depCode);
        return Result.ok();
    }

    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result findPageDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = signValidUtils.signValidation(request);
        String hosCode = (String) paramMap.get("hoscode");
        Integer page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        Integer limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hosCode);
        Page<Department> departmentPage = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(departmentPage);
    }

    @ApiOperation("上传科室接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = signValidUtils.signValidation(request);
        departmentService.saveDepartment(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = signValidUtils.signValidation(request);
        String hosCode = (String) paramMap.get("hoscode");
        Hospital hospital = hospitalService.getHospitalByHosCode(hosCode);
        return Result.ok(hospital);
    }

    @ApiOperation("上传医院接口")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 进行签名校验
        Map<String, Object> paramMap = signValidUtils.signValidation(request);
        // 传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        if (StringUtils.hasText(logoData)) {
            logoData = logoData.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        // 判断是否一致
        hospitalService.saveHospital(paramMap);
        return Result.ok();
    }
}
